package com.example.VolunteerBank.controller;

import com.example.VolunteerBank.pojo.Article;
import com.example.VolunteerBank.pojo.PageBean;
import com.example.VolunteerBank.pojo.Result;
import com.example.VolunteerBank.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/article")
@Slf4j
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    //新增文章
    @PostMapping
    public Result add(@RequestBody @Validated Article article){
        log.info("新增文章：{}", article);
        articleService.add(article);
        return Result.success();
    }

    //更新文章
    @PutMapping
    public Result update(@RequestBody @Validated Article article){
        log.info("更新文章：{}", article);
        articleService.update(article);
        return Result.success();
    }

    //获取文章详情
    @GetMapping("/detail")
    public Result<Article> queryById(Integer id){
        log.info("获取文章详情：{}", id);
        Article article = articleService.queryById(id);
        return Result.success();
    }

    //删除文章
    @DeleteMapping
    public Result delete(Integer id){
        log.info("删除文章：{}", id);
        articleService.delete(id);
        return Result.success();
    }

    //分页查询文章列表
    @GetMapping
    public Result<PageBean<Article>> list(Integer pageNum, Integer pageSize, @RequestParam(required = false) String categoryId, @RequestParam(required = false) String state){
        log.info("分页查询文章列表：{}，{}，{}，{}", pageNum, pageSize, categoryId, state);
        PageBean<Article> pageBean = articleService.page(pageNum, pageSize, categoryId, state);
        return Result.success(pageBean);
    }
}
